<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="Bookmark" href="/favicon.ico" >
    <link rel="Shortcut Icon" href="/favicon.ico" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="../js/html5shiv.js"></script>
    <script type="text/javascript" src="../js/respond.min.js"></script>
    <![endif]-->
    <link href="../css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../css/H-ui.css" />
    <link rel="stylesheet" type="text/css" href="../css/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../css/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <title></title>
</head>
<body>
<article class="page-container">
    <form action="" method="post" class="form form-horizontal" id="form-admin-role-add">

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>角色名称：</label>
            <div class="formControls col-xs-8 col-sm-10">
                <input type="text" class="input-text" value="" placeholder="" id="roleName" name="name">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">权限模块：</label>
            <div class="formControls col-xs-8 col-sm-10 checkBoxCon">

            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button class="btn btn-success radius submit" name=""><i class="icon-ok"></i> 确定</button>
            </div>
        </div>
    </form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/js/layer/2.4/layer.js"></script>
<script type="text/javascript" src="/js/H-ui.js"></script>
<script type="text/javascript" src="/js/H-ui.min.js"></script>
<script type="text/javascript" src="/js/postJson.js"></script>
<script type="text/javascript" src="/js/root.js"></script>
<!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<style>
    em{
        font-style: normal;
    }
</style>
<script type="text/javascript">



    var dataCollect=$('#search').serialize();
    var dataCollects = dataCollect.replace(/\+/g," ")
    dataCollects= decodeURIComponent(dataCollects,true);//防止中文乱码
    var jsondata=strJSON.formToJson(dataCollects);//转化为json
    var url = "/sysRole/allOperate";

    $.ajax({
        url:url,
        type:"get",
        async:false,
        success:function(msg){
            var htmlString = '';
            $.each(msg.data,function(i,n){
                // console.log(JSON.stringify(msg))

                if(n.parentId==0){
                    htmlString+='<dl class="permission-list">'
                    htmlString+='<dt>'
                    htmlString+='<label>'
                    htmlString+='<input type="checkbox" value="" name="" operateId="'+ n.id+'">'
                    htmlString+= n.name+'</label>'
                    htmlString+='</dt>'
                    htmlString+='<dd>'
                    htmlString+='<dl class="cl permission-list2">'
                    htmlString+='<dd>'
                    $.each(msg.data,function(i2,n2){
                        if(n.id == n2.parentId){
                            htmlString+='<span class="">'
                            htmlString+='<label class="">'
                            htmlString+='<input type="checkbox" value="" name="" operateId="'+n2.id+'">'
                            htmlString+=n2.name+'</label>'
                            htmlString+='<em class="null" nullMark="true" >'+"（"+'</em>'
                            $.each(msg.data,function(i3,n3){
                                if(n2.id == n3.parentId){
                                    htmlString+='<label class="">'
                                    htmlString+='<input type="checkbox" value="" name="" operateId="'+n3.id+'">'
                                    htmlString+=n3.name+'</label>'
                                }

                            })
                            htmlString+='<em class="null2">'+"）"+'</em>'
                            htmlString+='</span>'

                        }
                    })
                    htmlString+='</dd>'
                    htmlString+='</dl>'
                    htmlString+='</dd>'
                    htmlString+='</dl>'
                }

            })
            $(".checkBoxCon ").append(htmlString)

            $(".null").each(function(){
                //console.log($(this).next().hasClass("null2"))
                if($(this).next().hasClass("null2")==true){
                    $(this).addClass("hide")
                    $(this).next().addClass("hide")
                }
            })
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            var XmlStatus = XMLHttpRequest.status
//                         console.log(XMLHttpRequest.status)
            if(XmlStatus==401){
                alert(XmlError.error401)
                parent.window.location.href = "../login.html"
            }
            if(XmlStatus==403){
                alert(XmlError.error403)
            }
        }
    });


        $(".permission-list dt input:checkbox").click(function(){
            $(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
        });
        $(".permission-list2 dd input:checkbox").click(function(){
            var onCheck =$(this).parent().parent().find("input:checked").length;
            var notCheck=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if($(this).prop("checked")){
                $(this).parent().parent().find("label").first().find("input[type='checkbox']").prop("checked",true)//海宏
                $(this).closest("dl").find("dt input:checkbox").prop("checked",true);
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
            }
            else{
                $(this).parent().parent().find("label").first().find("input[type='checkbox']").prop("checked",false)//海宏
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);//海宏
                if(onCheck==0){
                    $(this).closest("dl").find("dt input:checkbox").prop("checked",false);
                }
                if(notCheck==0){
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
                }
            }
        });

        $(".submit").bind("click",function(){
            var roleName = $("#roleName").val();
            if(roleName==''){
                layer.msg("角色名称不能为空！",{icon:2,time:5000});
                $("#roleName").focus()
                return false;
            }else{
                var arr= {
                    role:{
                        "name":roleName
                    },
                    "operates":[]
                }
                var url = "/sysRole/addRole";
                $("input:checked").each(function(){
                    arr.operates.push($(this).attr("operateId"))
                })
                var arrPost = JSON.stringify(arr)
                $.ajax({
                    url:url,
                    contentType:"application/json",
                    data:arrPost,
                    type:"post",
                    async:false,
                    dataType:'json',
                    success:function(msg){
                        if(msg.success) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.msg("添加成功！",{icon:1,time:3000});
                            window.parent.frames.getData();
                            parent.layer.close(index);
                        }else {
                            var error = msg.errorMessage;
                            parent.layer.msg(error,{icon:2,time:3000});
                        }
                    },
                    error:function(XMLHttpRequest,textStatus,errorThrown){
                        var XmlStatus = XMLHttpRequest.status
//                         console.log(XMLHttpRequest.status)
                        if(XmlStatus==401){
                            alert(XmlError.error401)
                            parent.window.location.href = "../login.html"
                        }
                        if(XmlStatus==403){
                            alert(XmlError.error403)
                        }
                    }
                });
            }

        })
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>